﻿using System;
using System.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web.SessionState;

/// <summary>
/// Summary description for DeltaAsyncHandler
/// </summary>
public class DeltaAsyncHandler : IHttpHandler, IReadOnlySessionState
{

    public void ProcessRequest(HttpContext context)
    {

        _context = context;
        int startLineID = 0;
        int endLineID = 0;
        string bindingTableName = _context.Request.QueryString["bindingTable"];

        if (_context.Session[bindingTableName] == null)
        {
            //CompareFiles();
            startLineID = 0;
            endLineID = 50;
        }
        else
        {
            startLineID = Convert.ToInt32(_context.Request.QueryString["lastLineID"]);

            if (_context.Request.QueryString["nextLineID"] != null)
            {
                endLineID = Convert.ToInt32(_context.Request.QueryString["nextLineID"]) + 40;
            }
            else
            {
                endLineID = startLineID + 50;
            }
        }

        DataTable compareResults = (DataTable)_context.Session[bindingTableName];

        //The PrepareDataSet method stuffs the DataSet into a HTML table
        _context.Response.Write(BuildRows(compareResults, startLineID, endLineID));
    }

    private HttpContext _context;

    private string BuildRows(DataTable dt, int startLineID, int endLineID)
    {
        StringBuilder sb = new StringBuilder();
        DataRow[] rows = dt.Select("LineID > " + startLineID.ToString() + " AND LineID < " + endLineID.ToString());

        System.Data.DataRow dr;
        if (rows.Length > 0)
        {
            for (int i = 0; i < rows.Length; i++)
            {
                sb.Append("<tr class='tblRow'>");
                dr = rows[i];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    sb.Append("<td>" + dr[j] + "</td>");
                }
                sb.Append("</tr>");
            }
        }
        return sb.ToString();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}